package com.example.shiyan4;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class BindService extends Service {
    private int count;
    private boolean quit;


    private  MyBinder binder=new MyBinder();
    class MyBinder extends Binder {
         int getCount(){
            return count;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("Service is Binded");
        return binder;
    }

    public void onCreate(){
        super.onCreate();
        System.out.println("Service is Created");
        new Thread(){
            @Override
            public  void  run(){
                while (!quit){
                    try{
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e)
                    {

                    }
                    count++;
                }
            }
        }.start();
    }
    public boolean onUnbind(Intent intent){
        System.out.println("service is unbinded");
        return true;
    }
    public void onDestroy(){
        super.onDestroy();
        this.quit=true;
        System.out.println("Service is destroyed");
    }
}
